package com.aceona.demo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisTest {
	public static void main(String[] args) {
		
		//Redis连接池 
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(100000);
		config.setMaxIdle(10);
		config.setMinIdle(5);
		final JedisPool pool = new JedisPool(new JedisPoolConfig(), "172.19.1.122");
		
		
		ExecutorService service = Executors.newFixedThreadPool(100000);
		
		for(int j=1;j<100000;j++){
			
			Runnable task = new Runnable() {
				final Jedis jedis = pool.getResource();
				final String name = Thread.currentThread().getName();
				int i=1;
				@Override
				public void run() {
					jedis.select(1);
					while(i<1000000){
						System.out.println(name+"--->"+jedis.lindex("word", 1));
					}
					pool.returnResource(jedis);
					
				}
			};
			service.submit(task);
		}
		System.out.println("****************************************************");
//		
		//获取与Redis的连接
//		Jedis jedis = pool.getResource();
////		//选择数据库索引
//		jedis.select(1);
//		int i=1;
//		long start = System.currentTimeMillis(); 
//		while(i<=100000){
//			i++;
//			System.out.println(jedis.lindex("word", 1));
////			jedis.lindex("word", 2);
//		}
//		long end = System.currentTimeMillis();
//		System.out.println("耗时:"+(end-start));
//		Pipeline p = jedis.pipelined();
//		jedis.del("word","word1");
//		System.out.println(jedis.keys("*"));
//		System.out.println(jedis.lrange("word", 0, -1));
//		System.out.println(jedis.lindex("word", 1));
//        String word = "word"; 
//        String word1 = "word1"; 
//        jedis.lpush(word, "first");  
//        jedis.lpush(word, "second");  
////        jedis.lpush(word, "three"); 
////        jedis.rpush(word, "first");  
////        jedis.rpush(word, "second");  
////        jedis.rpush(word, "three"); 
//        jedis.sadd(word, "first");  
//        jedis.sadd(word, "second");  
//        jedis.sadd(word, "three"); 
//        jedis.sadd(word1, "werwerwe"); 
//        jedis.sadd(word1, "three"); 
//        jedis.sadd(word1, "AAA"); 
////        System.out.println("word : " + jedis.lrange(word, 0, -1));
////        System.out.println("info : " + jedis.info());
////        System.out.println("Length : " + jedis.llen(word));
//
//        //求交集
//        Set<String> set = jedis.sinter(word,word1);
//        System.out.println("交集:"+set);
//        if(set!=null){
//        	for(String key:set){
//        		System.out.println(key);
//        	}
//        }
//        jedis.info();
//        //求并集
//        Set<String> set1 = jedis.sunion(word,word1);
//        System.out.println("并集:"+set1);
//        if(set1!=null){
//        	for(String key:set1){
//        		System.out.println(key);
//        	}
//        }
//        //求差集
//        Set<String> set2 = jedis.sdiff(word1,word);
//        System.out.println("差集:"+set2);
//        if(set2!=null){
//        	for(String key:set2){
//        		System.out.println(key);
//        	}
//        }
//        System.out.println(jedis.keys("*"));
      
//		System.out.println("word : " + jedis.lrange(word, 0, -1));
//		System.out.println("word1 : " + jedis.lrange(word1, 0, -1));
//		String t = jedis.lpop(word);
//		while(t!=null){
//			System.out.println("取出队列word中的元素"+t);
//			t = jedis.lpop(word);
//		}
        
        //释放Redis连接
//        pool.returnResource(jedis);
//        jedis.lpop(word);
	}
}
